<?php

class Default_Model_UserMapper
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Default_Model_DbTable_User');
        }
        return $this->_dbTable;
    }

    public function save(Default_Model_User $user)
    {
        $data = array(
			'spagooy_user_first_name' => $user->getFirstname(),
			'spagooy_user_last_name' =>$user->getLastname(),
			'spagooy_user_sex' => $user->getSex(),
			'spagooy_user_language' => $user->getLanguage(),
			'spagooy_user_email' => $user->getEmail(),
			'spagooy_user_username' => $user->getUsername(),
			'spagooy_user_password' => $user->getPassword(),
			'spagooy_user_role' => $user->getRole(),
			'spagooy_user_active' => $user->getActive()
        );

        if (null === ($id = $user->getId())) {
            unset($data['spagooy_user_id']);
            return $this->getDbTable()->insert($data);
		} else {
            $this->getDbTable()->update($data, array('spagooy_user_id = ?' => $id));
        }
    }
	
	public function activateUser($data, $userId){
		return $this->getDbTable()->activateUser($data, $userId);
	}
	
	public function emailExists($email) {
		return $this->getDbTable()->emailExists($email);
	}
	
	public function usernameExists($username) {
		return $this->getDbTable()->usernameExists($username);
	}
	
	public function numberOfUsers()
	{
		return $this->getDbTable()->numberOfUsers();
	}
	
/*
    public function find($id, Default_Model_Guestbook $guestbook)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $guestbook->setId($row->id)
                  ->setEmail($row->email)
                  ->setComment($row->comment)
                  ->setCreated($row->created);
    }

    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Default_Model_Guestbook();
            $entry->setId($row->id)
                  ->setEmail($row->email)
                  ->setComment($row->comment)
                  ->setCreated($row->created)
                  ->setMapper($this);
            $entries[] = $entry;
        }
        return $entries;
    }
*/

}
